var searchField;
var searchKey;

$(document).ready(function(){
	addFadeEffect();
});


function searchCustomer() {
	var pattern = /^[\s]+$/;
	if ($("#customer_keyword").val() == "" || pattern.test($("#customer_keyword").val())) {
		return;
	}
	
	searchField = $("#search_field").val();
	searchKey = $("#customer_keyword").val();
	
	jumpPage_search_customer(1);
}

function jumpPage_search_customer(targetPage) {
	var requestUrl = "search_customer";
	
	var params = {
		page:targetPage,
		pageSize:5
	};
	switch (searchField) {
		case "1":
			params.name = searchKey;
			break;
		case "2":
			params.mobile = searchKey;
			break;
		case "3":
			params.license_number = searchKey;
			break;
		case "4":
			params.model = searchKey;
			break;
		default:
			break;
	}
	$.post(requestUrl, params, function(ret){
		$("#div_noresult").css("visibility", "hidden");
		$("#div_result").css("visibility", "visible");
		$("#div_search_result").html("");
		var newHtml = "";
		var item;
		var pages = 1;
		
		customers = ret.resultData.items;
		
		//显示结果列表
		for (var i = 0; ret.resultData.items && i < ret.resultData.items.length; i++) {
			item = ret.resultData.items[i];
			newHtml += '<div class="order_item" style="padding-top: 2px" onmouseover="selectMaintainItem(this);" onclick="showUserInfo(' + i + ', \'customer_list\');">';
			newHtml += '	<div class="col_name">' + item.name + '</div>';
			newHtml += '	<div class="col_license">' + item.carLicense + '</div>';
			newHtml += '	<div class="col_model">' + item.carModel + '</div>';
			newHtml += '	<div class="col_mobile">' + item.mobile + '</div>';
			newHtml += '	<div style="clear: both;"></div>';
			newHtml += '</div>';
		}
		
		//显示分页信息
		if (ret.resultData.total_num && ret.resultData.total_num > 5) {
			pages = Math.ceil(ret.resultData.total_num/5);
			var page = new Page(pages);
			page.currentPage = targetPage;
			newHtml += page.genPageHtml("jumpPage_search_customer");
		}
		
		$("#div_search_result").html(newHtml);
		customerListHtml = $("#div_top_tabhost").html();
		
		if (newHtml == "") {
			$("#div_noresult").css("visibility", "visible");
			$("#div_result").css("visibility", "hidden");
		}
	}, dataType="json");
}